package resultHandler;

import org.apache.commons.dbutils.ResultSetHandler;
import pojo.Column;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MyColumnListHandler implements ResultSetHandler {
    @Override
    public List<Column> handle(ResultSet resultSet) throws SQLException {
        List<Column> list = new ArrayList<>();
        while (resultSet.next()) {
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            if (columnCount != 6) {
                throw new RuntimeException("数据列数错误");
            }
            Column column = new Column();
            column.setField(resultSet.getString("Field"));
            column.setType(resultSet.getString("Type"));
            column.setIsNull(resultSet.getString("Null"));
            column.setKey(resultSet.getString("Key"));
            column.setDefaultValue(resultSet.getString("Default"));
            column.setExtra(resultSet.getString("Extra"));
            list.add(column);
        }
        return list;
    }
}
